package thread;

public class SyncDemo1 {
    public static boolean  success1=false;
    public static boolean  success2=false;

    public static void main(String[] args) {
        Bank bank=new Bank();
        int num=1;
        while(true) {
            Thread t1 = new Thread() {
                public void run() {
                    success1 = bank.getMoney(2000000);
                }
            };
            Thread t2 = new Thread() {
                public void run() {
                    success2 = bank.getMoney(2000000);
                }
            };
            t1.start();
            t2.start();

            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (success1 && success2) {
                System.out.println("两个人都取款成功，出现来了并发问题！");
                System.out.println("共测试了："+num+"次");
                break;
            } else {
                num++;
                System.out.println("只有一人取款成功");
                bank.saveAccount(2000000);
                success1 = false;
                success2 = false;
            }
        }
    }
}
